<?php
class WebPageCategory extends Model {
	var $_table = 'web_page_category';
	
	var $cid_nak; // INT UNSIGNED NOT NULL AUTO_INCREMENT,
	var $up_cid_nr = 0; // INT NOT NULL DEFAULT 0 COMMENT '上级栏目',
	var $page_id_nr = 0; // INT 发布后对应的页面编号
	var $title_sr; // VARCHAR(32) NOT NULL COMMENT '栏目名称',
	var $name_sr; // VARCHAR(32) NOT NULL COMMENT '引用名，用于模板中',
	var $type_sr; // VARCHAR(16) NOT NULL COMMENT '分类类型',
	var $display_nr = 1; // '是否显示在主导航上',
	var $sort_nr = 0; // SMALLINT(8) UNSIGNED NOT NULL COMMENT '导航时的排序',
	var $url_path_sr; // VARCHAR(64) NOT NULL COMMENT '栏目默认存储路径，相对于根目录',
	var $url_pattern_sr; // VARCHAR(128) NOT NULL COMMENT '栏目下页面的默认url生成规则',
	var $url = ''; // 发布后的URL
	var $bind = ''; // 绑定的二级域名
	var $home_nr = 0; //是否是主页
	var $note_s = ''; // VARCHAR(256) NULL COMMENT '备注',
	

	public function check_name($name, $data) {
		$where ['name'] = $value;
		if (! empty ( $data ['cid'] )) {
			$where ['cid !='] = $data ['cid'];
		}
		return $this->exist ( $where ) ? "栏目引用名已经存在" : true;
	}
	
	public function check_path($path, $data) {
		$where ['url_path'] = $value;
		if (! empty ( $data ['cid'] )) {
			$where ['cid !='] = $data ['cid'];
		}
		return $this->exist ( $where ) ? "栏目引用名已经存在" : true;
	}
	/**
	 * 根据栏目的url_pattern生成url
	 *
	 * @param string $category        	
	 * @param int/array $page
	 * 页面ID
	 * @return string
	 */
	public function generateUrl($category, $page) {
		if (is_numeric ( $page )) {
			$pM = new WebPageModel ();
			$page = $pM->read ( $page );
		}
		$cate = $this->where ( array ('name' => $category ) )->retrieve ( "*", 0 );
		if (! $cate) {
			$cate ['url_pattern'] = '{Y}{m}{d}/{id}.html';
		}
		$search [] = '{id}';
		$replace [] = $page ['page_id'];
		
		$search [] = '{Y}';
		$replace [] = date ( 'Y' );
		
		$search [] = '{y}';
		$replace [] = date ( 'y' );
		
		$search [] = '{m}';
		$replace [] = date ( 'm' );
		
		$search [] = '{d}';
		$replace [] = date ( 'd' );
		
		$search [] = '{R}';
		$replace [] = rand ( 1000, 9999 );
		
		$search [] = '{uid}';
		$replace [] = $page ['create_uid'];
		
		$search [] = '{M}';
		$replace [] = ! empty ( $page ['page_mode'] ) ? $page ['page_mode'] : 'article';
		
		$search [] = '{t}';
		$replace [] = ! empty ( $page ['page_type'] ) ? urlencode ( $page ['page_type'] ) : 'page';
		
		$search [] = '{cp}';
		$replace [] = ! empty ( $cate ['url_path'] ) ? $cate ['url_path'] : $cate ['name'];
		
		$search [] = '{n}';
		$replace [] = $cate ['name'];
		
		$search [] = '{p}';
		$replace [] = Pinyin::c ( $page ['title'] );
		
		$search [] = '{P}';
		$replace [] = Pinyin::c ( $page ['title'], true );
		
		$urlp = ! empty ( $cate ['url_pattern'] ) ? $cate ['url_pattern'] : '{cp}/{Y}/{m}/{d}/{id}.html';
		return str_replace ( $search, $replace, $urlp );
	}
	/**
	 * 得到分类标题
	 *
	 * @param string $category        	
	 * @return string
	 */
	public function getCategoryTitle($category) {
		if (empty ( $category )) {
			return '';
		}
		$title = $this->get ( 'title', array ('name' => $category ) );
		return $title ? $title : '';
	}
	/**
	 * 得到分类标题与url
	 *
	 * @param string $category        	
	 * @return string
	 */
	public function getCategoryInfo($category) {
		if (empty ( $category )) {
			return '';
		}
		$title = $this->where ( array ('name' => $category ) )->retrieve ( 'title,url,cid,bind,home as domain_home', 0 );
		return $title ? $title : false;
	}
	public function getChildCateNames($category) {
		$cate = $this->get ( 'cid', array ('name' => $category ) );
		if ($cate) {
			$cates = $this->treeData ( 'up_cid', $cate, 'cid', 0, 'name', false, false, false );
		}
		$cates [] = $category;
		return $cates;
	}
	public function getTreeNodes($cid) {
		$nodes = array (array ('id' => 0, 'pId' => - 1, 'cate' => '', 'name' => '顶级栏目', 'isParent' => true, 'open' => true ) );
		$this->_getTreeNodes ( 0, $cid, $nodes );
		return $nodes;
	}
	/**
	 * 
	 * 保存频道栏目
	 * @param array $category
	 */
	public function saveCategory($category) {
		$rst = $this->save ( $category );
		if ($rst && ! empty ( $rst ['bind'] )) {
			if ($rst ['home']) {
				$this->update ( array ('home' => 0 ), array ('bind' => $rst ['bind'], 'cid <>' => $rst ['cid'] ) );
			}
			$this->updateSubcateBind ( $rst ['cid'], $rst ['bind'] );
		}
		return $rst;
	}
	
	private function _getTreeNodes($up_cid, $cid, &$nodes) {
		$cates = $this->where ( array ('up_cid' => $up_cid ) )->retrieve ( 'cid,up_cid,title,name' );
		
		if ($cates) {
			foreach ( $cates as $cate ) {
				if ($cid == $cate ['cid']) {
					continue;
				}
				$nodes [] = array ('id' => $cate ['cid'], 'cate' => $cate ['name'], 'pId' => $cate ['up_cid'], 'name' => $cate ['title'] );
				$this->_getTreeNodes ( $cate ['cid'], $cid, $nodes );
			}
		}
	}
	private function updateSubcateBind($cid, $bind) {
		$cates = $this->where ( array ('up_cid' => $cid ) )->retrieve ( 'cid' );
		if ($cates) {
			foreach ( $cates as $cate ) {
				$this->update ( array ('bind' => $bind ), $cate ['cid'] );
				$this->updateSubcateBind ( $cate ['cid'], $bind );
			}
		}
	}
}